package com.example.petstore.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.petstore.entity.Collection;
import com.example.petstore.entity.Product;
import com.example.petstore.entity.VO.CollectionVO;
import com.example.petstore.exception.BaseException;
import com.example.petstore.mapper.CollectionMapper;
import com.example.petstore.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.petstore.service.IProductService;
import com.example.petstore.utils.BaseContext;
import com.example.petstore.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DD
 * @since 2024-07-04
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Autowired
    private ICollectionService collectionService;

    private IProductService productService;

    @Override
    public Result<String> addCommodity(int id) {

        Product product = productService.getById(id);
        if (BeanUtil.isEmpty(product))
            throw new BaseException("添加失败");

//        BeanUtils.copyProperties(product,collection);

        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("purchaser_id",BaseContext.getCurrentId());
        collectionQueryWrapper.eq("shop_type",product.getPetType());
        Collection one = collectionService.getOne(collectionQueryWrapper);

        if (BeanUtil.isEmpty(one))
        {
            Collection collection = new Collection();
            collection.setCount(1);
            collection.setPrice(product.getPrice());
            collection.setPurchaserId(BaseContext.getCurrentId().toString());
            collection.setShopType(product.getPetType());
            collection.setShopId(product.getShopId());
            collectionService.save(collection);
        }
        else {
            int count=one.getCount()+1;
            one.setCount(count);
            one.setPrice(count*product.getPrice());
            collectionService.save(one);
        }
        return Result.success("添加成功");
    }

    @Override
    public Result<String> subCommodity(int id) {
        Product product = productService.getById(id);
        if (BeanUtil.isEmpty(product))
            throw new BaseException("减少失败");

//        BeanUtils.copyProperties(product,collection);

        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("purchaser_id",BaseContext.getCurrentId());
        collectionQueryWrapper.eq("shop_type",product.getPetType());
        Collection one = collectionService.getOne(collectionQueryWrapper);

        if (BeanUtil.isEmpty(one))
        {
           throw new BaseException("减少失败");
        }
        else {
            if(one.getCount()==1){
                collectionService.remove(collectionQueryWrapper);
            }
            else {
                int count =one.getCount()-1;
                one.setCount(count);
                one.setPrice(count*product.getPrice());
                collectionService.save(one);
            }
        }
        return Result.success("减少成功");
    }

    @Override
    public Result getAll(int current,int size) {

        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();

//        collectionQueryWrapper.eq("purchaserId",BaseContext.getCurrentId());
//        List<Collection> list = collectionService.list(collectionQueryWrapper);

        CollectionVO collectionVO = new CollectionVO();

        Page<Collection> collectionPage = new Page<>(current, size);
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.eq(Collection::getPurchaserId,BaseContext.getCurrentId());
        this.page(collectionPage,queryWrapper);

//        BeanUtils.copyProperties(collectionPage,,"records");

        List<Collection> records = collectionPage.getRecords();


        collectionVO.setList(records);
        int sum = records.stream().mapToInt(Collection::getPrice).sum();
        collectionVO.setAllPrice(sum);
        return Result.success(collectionVO);
    }
}
